<?php

//Route::get('/', 'HomeController@hamcangoi');
Route::resource('/', 'HomeController'); //dùng {{Asset('')}}
Route::resource('/ghichu', 'ArticleController'); // dùng link_to_route ghichu.show | ghichu.update

Route::group(array('prefix' => 'admin'), function() {
	Route::resource('/', 'admin\AdminController');
        Route::get('chuong/updownchuong/{idmonhoc}/{upordown}/{swapbeginid}', 'admin\MonHocController@swapchuong');
	Route::get('chuong/battatchuong/{onoroff}/{id}', 'admin\MonHocController@battatchuong');
        Route::get('chuong/delete/{id}', 'admin\MonHocController@xoachuong');
        
        
	Route::get('monhoc/updown/{upordown}/{swapbeginid}', 'admin\MonHocController@swap');
        Route::get('monhoc/battat/{onoroff}/{id}', 'admin\MonHocController@battat');
	Route::get('monhoc/crud/xoa', 'admin\MonHocController@xoatatca');
    
    /**
    * B: Thêm và chỉnh sửa chương
    */
    Route::get('monhoc/chuong/themchuong', 'admin\MonHocController@themchuong');
    Route::post('monhoc/chuong/capnhatchuong', 'admin\MonHocController@capnhatchuong');
    /**
    * E: Thêm và chỉnh sửa chương
    */

	Route::resource('monhoc', 'admin\MonHocController');//
	Route::resource('chuong', 'admin\ChuongController');
	Route::resource('ghichu', 'admin\GhiChuController');
	Route::resource('dashboard', 'admin\DashBoardController');
        
});

// Xác thực đăng nhập 
Route::post('/login',function(){
	if(User::check_login(Input::get("user_input"),md5(sha1(Input::get("password"))))){
		Session::put("logined","true");
		Session::put("account",User::where("username","=",Input::get("user_input"))->first()->fullname);
		return View::make('admin.admin');
	}
	else
		return View::make('admin.login')->with('error_message','Vui lòng kiểm tra Tài khoản và Mật khẩu.');
});
//Logout
Route::get('/logout',function(){
	Session::forget("logined");
	Session::forget("account");
	return View::make('admin.login');
});


// Route::resource('/monhoc', 'MonHocController');
// Route::controller('/monhoc', 'MonHocController');
/*
 Quản trị Area
 ---------------------------------------------------
*/
// Route::filter('check-login',function(){
// 	if(!Session::has('logined'))
// 		return View::make('admin.login');
// });
// Route::resource('/admin', 'AdminController');
// Route::get('/admin', array("before"=>"check-login",function(){
// 	return View::make('admin.admin');
// }));
//Khi chọn chức năng

 
Route::get('/editprofile',array("before"=>"check-login",function(){
	return View::make('editprofile');
}));

//  Route::get('/admin/action/{chucnang}', function($chucnang)
// {	
// 	return View::make("admin.admin")->with('chuc_nang',$chucnang);
// });
/*
---------------------------------------------------
*/

// Route::get('/', function()
// {
// 	return View::make('home');
// });

Route::get('/article', function()
{
	return View::make('article');
});


Route::get('/register',function(){
	return View::make('register');
});

Route::post('/register',function(){
	$rules=array("username"=>"required|min:3");
	if(!Validator::make(Input::all(),$rules)->fails()){
		$user = new User();
		$user->username = Input::get("username");
		$user->password = md5(sha1(Input::get("username")));
		$user->email = Input::get("email");
		$user->ck = Input::get("txtDescription");
		$user ->save();
		Session::put("register_success",Input::get('username')."Đã đăng kí thành công");

		return Redirect::to("login"); 
	}
});
 
Route::get('/login',function(){
	return View::make('login');
});




Route::get('/editprofile',array("before"=>"check-login",function(){
	return View::make('editprofile');
}));



// Route::post('/login',function(){
// 	if(User::check_login(Input::get("user_input"),md5(sha1(Input::get("password"))))){
// 		Session::put("logined","true");
// 		return Redirect::to('editprofile');
// 	}
// 	else
// 		return View::make('login')->with('error_message','kiem tra lai');
// });
 

 Route::group(array("prefix"=>"check"),function(){
    Route::post('/check_tag_id',function(){
		if(MonHoc::check_tag_id(Input::get("TAG_ID"))){
			return "true";
		}
		else {
			return "false";
		}
	});

 	Route::post('/check-username',function(){
		if(User::check_username(Input::get("username"))){
			return "true";
		}
		else {
			return "false";
		}
	});
	Route::post('/check-email',function(){
		if(User::check_email(Input::get("email"))){
			return "true";
		}
		else {
			return "false";
		}
	});
 });

